package com.dianrui.agent.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dianrui.agent.R;
import com.dianrui.agent.bean.MsgList;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MessageListAdapter extends BaseAdapter {
    Context context;
    List<MsgList> msgLists;
    public MessageClickListener msgClickListener;

    public MessageListAdapter(Context context, List<MsgList> msgLists) {
        this.context = context;
        this.msgLists = msgLists;
    }

    public void refresh(List<MsgList> msgLists) {
        this.msgLists = msgLists;
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return msgLists.size();
    }

    @Override
    public Object getItem(int position) {
        return msgLists.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public interface MessageClickListener {
        void onMessagerDelete(String msgId);
        void onMessageRead(String msgId,TextView title,TextView createtime,TextView contents,TextView show_reddot);
    }

    public void setOnButtonClickListener(MessageClickListener messageClickListener) {
        this.msgClickListener = messageClickListener;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_message, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        if (msgLists.size()>0) {
            final MsgList msgList = msgLists.get(position);
            holder.title.setText(msgList.title);
            holder.createTime.setText(msgList.create_time);
            holder.contents.setText(msgList.contents);
            if ("1".equals(msgList.is_read)) {
                holder.title.setTextColor(context.getResources().getColor(R.color.gray));
                holder.createTime.setTextColor(context.getResources().getColor(R.color.gray));
                holder.contents.setTextColor(context.getResources().getColor(R.color.gray));
                holder.show_reddot.setVisibility(View.GONE);//已读的不显示红点
            }else {
                holder.title.setTextColor(context.getResources().getColor(R.color.black));
                holder.createTime.setTextColor(context.getResources().getColor(R.color.black));
                holder.contents.setTextColor(context.getResources().getColor(R.color.black));
                holder.show_reddot.setVisibility(View.VISIBLE);
            }
            holder.clickMessage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    msgClickListener.onMessageRead(msgList.messages_id,holder.title,holder.createTime,holder.contents,holder.show_reddot);
                }
            });
            holder.clickMessage.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    msgClickListener.onMessagerDelete(msgList.messages_id);
                    return true;
                }
            });

        }
        return convertView;
    }

     class ViewHolder {
        @BindView(R.id.title)
        TextView title;
        @BindView(R.id.create_time)
        TextView createTime;
        @BindView(R.id.contents)
        TextView contents;
         @BindView(R.id.show_msg_reddot)
         TextView show_reddot;
        @BindView(R.id.click_message)
        LinearLayout clickMessage;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
